﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

#if WINDOWS
using System.Drawing;
using System.Windows.Forms;
#endif

namespace Convection
{
    public class XboxGamePad
    {
        public XboxGamePad(PlayerIndex pIndex)
        {
            mPlayerIndex = pIndex;
            mVibration = new GamePadVibration(pIndex);
        }

        #region Methods

        internal void Update()
        {
            mLastState = mCurrentState;
            mCurrentState = GamePad.GetState(mPlayerIndex);
        }

        #endregion

        #region Properties

        #region Internal Variables

        internal GamePadState mCurrentState;
        internal GamePadState mLastState;
        internal PlayerIndex mPlayerIndex;
        internal GamePadVibration mVibration;

        #endregion

        public bool ButtonDown(Buttons button)
        {
            return CurrentState.IsButtonDown(button);
        }
        public bool ButtonPressed(Buttons button)
        {
            bool returnBool = false;
            if (CurrentState.IsButtonDown(button) & (CurrentState.IsButtonDown(button) != LastState.IsButtonDown(button)))
            {
                returnBool = true;
            }
            return returnBool;
        }
        public GamePadButtons Buttons
        {
            get
            {
                return CurrentState.Buttons;
            }
        }
        public bool ButtonUp(Buttons button)
        {
            return CurrentState.IsButtonUp(button);
        }
        public GamePadCapabilities Capabilities
        {
            get
            {
                return GamePad.GetCapabilities(mPlayerIndex);
            }
        }
        public GamePadType ControllerType
        {
            get
            {
                return GamePad.GetCapabilities(mPlayerIndex).GamePadType;
            }
        }
        public GamePadState CurrentState
        {
            get
            {
                return mCurrentState;
            }
        }
        public GamePadDPad DPad
        {
            get
            {
                return CurrentState.DPad;
            }
        }
        public bool IsConnected
        {
            get
            {
                return CurrentState.IsConnected;
            }
        }
        public GamePadState LastState
        {
            get
            {
                return mLastState;
            }
        }
        public GamePadThumbSticks ThumbSticks
        {
            get
            {
                return CurrentState.ThumbSticks;
            }
        }
        public GamePadTriggers Triggers
        {
            get
            {
                return CurrentState.Triggers;
            }
        }
        public GamePadVibration Vibration
        {
            get
            {
                return mVibration;
            }
        }

        #endregion
    }

    public class GamePadVibration
    {
        public GamePadVibration(PlayerIndex pIndex)
        {
            mPlayerIndex = pIndex;
        }

        #region Methods

        internal void UpdateVibrate()
        {
            GamePad.SetVibration(mPlayerIndex, mLeftMotor, mRightMotor);
        }

        public void StopVibrate()
        {
            mLeftMotor = 0;
            mRightMotor = 0;
            UpdateVibrate();
        }

        #endregion

        #region Properties

        #region Internal Variables

        internal float mLeftMotor;
        internal PlayerIndex mPlayerIndex;
        internal float mRightMotor;

        #endregion

        public bool IsVibrating
        {
            get
            {
                bool returnBool = false;
                if (LeftMotor > 0 | RightMotor > 0)
                {
                    returnBool = true;
                }
                return returnBool;
            }
        }
        public float LeftMotor
        {
            get
            {
                return mLeftMotor;
            }
            set
            {
                mLeftMotor = value;
                UpdateVibrate();
            }
        }
        public float RightMotor
        {
            get
            {
                return mRightMotor;
            }
            set
            {
                mRightMotor = value;
                UpdateVibrate();
            }
        }

        #endregion
    }
}
